package cn.tangsihai.basic.service.impl;

import cn.tangsihai.basic.mapper.BasicMapper;
import cn.tangsihai.basic.query.BasicQuery;
import cn.tangsihai.basic.service.IBasicService;
import cn.tangsihai.basic.util.PageList;
import cn.tangsihai.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * service层基础crud，批量操作，分页查询的实现类
 * 注：泛型的使用
 * @param <T>
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {
    @Autowired
    private BasicMapper<T> basicMapper;

    @Override
    public T findById(Long id) {
        return basicMapper.loadById(id);
    }

    @Override
    public List<T> findAll() {
        return basicMapper.loadAll();
    }

    @Override
    @Transactional
    public void save(T t) {
        basicMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        basicMapper.remove(id);
    }

    @Override
    public PageList<T> queryList(BasicQuery query) {
        // 查询分页+高级查询起始页数据
        Integer totals = basicMapper.querycount(query);
        // 查询分页+高级查询每页数据
        List<T> rows = basicMapper.queryData(query);
        return new PageList<>(totals, rows);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        basicMapper.patchDelete(ids);
    }
}
